import * as userApi from "@/api/user";
export default {
    namespaced: true, // 开启命名空间
    state: {
        loading: false,
        user: null
    },
    getters: { // 可以认为是 store 的计算属性
        status(state) {
            if (state.loading) {
                return "loading";
            } else if (state.user) {
                return "login";
            } else {
                return "unlogin";
            }
        }
    },

    mutations: {
        setLoading(state, payload) {
            state.loading = payload
        },
        setUser(state, payload) {
            state.user = payload
        }
    },

    actions: {
        async login(ctx, payload) {
            ctx.commit("setLoading", true);
            const user = await userApi.login(payload.loginId, payload.loginPwd);
            ctx.commit("setUser", user);
            ctx.commit("setLoading", false);
            return user;
        },
        async whoAmI(ctx) {
            ctx.commit("setLoading", true);
            const user = await userApi.whoAmI();
            ctx.commit("setUser", user);
            ctx.commit("setLoading", false);
        },
        async logout(ctx) {
            ctx.commit("setLoading", true);
            await userApi.loginOut();
            ctx.commit("setUser", null);
            ctx.commit("setLoading", false);
        }
    }
}